package com.xinzhi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinzhi.model.Curriculum_vitae;
import com.xinzhi.model.Result;
import com.xinzhi.service.ICurriculum_vitaeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class Curriculum_vitaeControlller {

    @Autowired
    private ICurriculum_vitaeService iCurriculum_vitaeService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(Integer page) {
        if (page > 0) {
            page = page * (page - 1);
        } else {
            page = -1;
        }
        PageHelper.startPage(page, 2);
        List<Curriculum_vitae> findall = iCurriculum_vitaeService.findAll(page);

        PageInfo<Curriculum_vitae> pageInfo = new PageInfo<>(findall);

        int pages = pageInfo.getPages();

        if (pages < page || page == -1) {
            return Result.error("404");
        }
        List<Curriculum_vitae> list = pageInfo.getList();
        for (Curriculum_vitae company1 : list) {
            System.out.println(company1);
        }
        return Result.ok();
    }

    @RequestMapping("/updatevitae")
    @ResponseBody
    public Result updatevitae(Curriculum_vitae curriculum_vitae) {
        int updatevitae = iCurriculum_vitaeService.updatevitae(curriculum_vitae);
        if (updatevitae > 0) {
            return Result.ok();
        }
        return Result.error(404, "修改失败");
    }

    @RequestMapping("/addvitae")
    @ResponseBody
    public Result addvitae(Curriculum_vitae curriculum_vitae) {
        int addvitae = iCurriculum_vitaeService.addvitae(curriculum_vitae);
        if (addvitae > 0) {
            return Result.ok();
        }
        return Result.error(404, "添加失败");
    }


    @RequestMapping("/deletevitae")
    @ResponseBody
    public Result deletevitae(String name, String sex) {
        int deletevitae = iCurriculum_vitaeService.deletevitae(name);
        if (deletevitae > 0) {
            return Result.ok();
        }
        return Result.error(404, "删除失败");
    }
}
